Graphene strongly ties to Arquillian Core and its extensions Containers and Drone to manage lifecycle of the application server, selenium server and Graphene.
In the common scenarios, you need only to choose container you would like to deploy application into and the framework you would like to use for testing.
Project Setup
Maven Project Dependencies
In order to setup project dependencies, you need to define library dependencies.
Refer to Framework Integration Options for more details.
Starting with testing from Java
The simplest possible test would look like this:
@RunWith(Arquillian.class)
public class BasicTestCase {
@Drone
WebDriver driver;
@Test
public void testOpeningHomePage() {
driver.get("http://www.google.com");
String pageTitle = driver.getTitle();
Assert.assertEquals(pageTitle, "Google");
}
}
This test uses @Drone annotation in order to bring browser to your test case using dependency injection.
You can read more about Using Drone, where you will get information which instances you can use in which situations.
The browser is represented by WebDriver class instance (or its implementations).
For more information what is WebDriver and how it relates to Graphene, look at Using WebDriver.
This is really simple case, it does not really show where Graphene excels.
Advanced Use Cases
Page Abstractions
On the high level, Graphene allows you to create reusable abstractions in form of Page Object (the pattern widely known in Selenium community)
and Page Fragments, which allow you to encapsulate smaller pieces of your page like JavaScript widgets, UI components or simply forms.
@RunWith(Arquillian.class)
public class TestLogin {
@Drone
WebDriver browser;
@Page
HomePage homePage;
@Test(expects = LoginFailedException.class)
public void testLoginFailed()
homePage.login("non-existent", "user");
}
}
public class HomePage {
@FindBy(".login-form")
LoginDialog loginDialog;
@FindBy(".search")
AutocompleteComponent fulltextSearch;
@FindBy(".events")
CalendarComponent eventCalendar;
/**
* @throws LoginFailedException when login fails
*/
public void login(String user, String password) {
loginDialog.setUser(user);
loginDialog.setPassword(password);
loginDialog.login();
}
public void switchToEvent(Date date) {
eventCalendar.clickOnDate(date);
}
}
You can read more about using reusable Page Abstractions.
Testing AJAX
Second big promise of Graphene is making a testing of applications using asynchronous calls heavily (AJAX, WebSocket, ...) simple task.
Graphene allows that by exposing compact syntax for describing conditions which must be met on the page once asynchronous call finished.
@Test
public void testRegistrationForm()
guardXhr(nameInput).sendKeys("John");
assertEquals(nameMessage, "The username was already registered");
}
@Test
public void testRegistrationForm() {
nameInput.sendKeys("John");
waitAjax(element(nameMessage).textContains("already registered"));
}
You can read more about Testing AJAX,
or you can go directly to the documentation of features, which enables that:
Request Guards
Request Guardsbrings simplest conditions: wait for request to be finished (blocking test)
Under the Hood
Graphene enables way more features on a low level.
Using following features may help you write outstanding tests:
Graphene Context
allows you to obtain current thread-local context of the browser and "inject" it where you need it without reference propagation.
WebDriver browser = GrapheneContext.getContextFor(Default.class).getWebDriver();
JavaScript Interface
allows to call JavaScript functions from Java directly.
@JavaScript
public interface Document {
String getTitle();
}
Page Extensions
allows to bring JavaScript code to the browser.
@Dependency("requestGuard.js")
@JavaScript("graphene.requestGuard")
public interface RequestGuard {
RequestType getRequestDone();
}
Using Drone
Drone is extension for Arquillian that manages lifecycle of the Selenium Server and Selenium clients as Selenium 1.x, Selenium 2.x and Graphene.
Refer to Using Drone section for more information.
Running Graphene tests from an IDE
It is possible to run Graphene tests from any modern IDE. See Arquillian Getting Started Guide for more information about how to run and debug tests in Eclipse.